// utils/simple-request-test.js
// 简单网络请求测试工具

class SimpleRequestTest {
  constructor() {
    this.testUrl = 'https://www.baidu.com'
  }

  // 测试1: 使用Promise包装wx.request
  async testPromiseRequest() {
    console.log('=== 测试1: Promise包装wx.request ===')
    
    try {
      const response = await wx.request({
        url: this.testUrl,
        method: 'GET',
        timeout: 10000
      })
      
      console.log('Promise响应:', response)
      console.log('响应类型:', typeof response)
      console.log('响应键名:', Object.keys(response))
      console.log('状态码:', response.statusCode)
      
      return {
        success: true,
        method: 'promise',
        response: response,
        statusCode: response.statusCode
      }
    } catch (error) {
      console.log('Promise请求失败:', error)
      return {
        success: false,
        method: 'promise',
        error: error.message
      }
    }
  }

  // 测试2: 使用回调方式
  testCallbackRequest() {
    console.log('=== 测试2: 回调方式wx.request ===')
    
    return new Promise((resolve, reject) => {
      wx.request({
        url: this.testUrl,
        method: 'GET',
        timeout: 10000,
        success: (res) => {
          console.log('回调成功:', res)
          console.log('响应类型:', typeof res)
          console.log('响应键名:', Object.keys(res))
          console.log('状态码:', res.statusCode)
          
          resolve({
            success: true,
            method: 'callback',
            response: res,
            statusCode: res.statusCode
          })
        },
        fail: (err) => {
          console.log('回调失败:', err)
          resolve({
            success: false,
            method: 'callback',
            error: err
          })
        }
      })
    })
  }

  // 测试3: 使用requestTask
  testRequestTask() {
    console.log('=== 测试3: requestTask方式 ===')
    
    return new Promise((resolve, reject) => {
      const requestTask = wx.request({
        url: this.testUrl,
        method: 'GET',
        timeout: 10000,
        success: (res) => {
          console.log('requestTask成功:', res)
          resolve({
            success: true,
            method: 'requestTask',
            response: res,
            statusCode: res.statusCode
          })
        },
        fail: (err) => {
          console.log('requestTask失败:', err)
          resolve({
            success: false,
            method: 'requestTask',
            error: err
          })
        }
      })
      
      // 监听请求进度
      requestTask.onHeadersReceived((res) => {
        console.log('requestTask收到响应头:', res)
      })
      
      requestTask.onProgressUpdate((res) => {
        console.log('requestTask进度:', res)
      })
    })
  }

  // 测试4: 测试华为云Token请求
  testHuaweiTokenRequest() {
    console.log('=== 测试4: 华为云Token请求 ===')
    
    const tokenUrl = 'https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens'
    const tokenRequest = {
      auth: {
        identity: {
          methods: ['password'],
          password: {
            user: {
              name: 'ilieyun',
              password: 'Lieyun@2090',
              domain: {
                name: 'ilieyun'
              }
            }
          }
        },
        scope: {
          project: {
            name: 'cn-north-4'
          }
        }
      }
    }
    
    return new Promise((resolve, reject) => {
      console.log('发送华为云Token请求...')
      
      const requestTask = wx.request({
        url: tokenUrl,
        method: 'POST',
        data: tokenRequest,
        header: {
          'Content-Type': 'application/json'
        },
        timeout: 15000,
        success: (res) => {
          console.log('华为云Token请求成功:', res)
          console.log('响应类型:', typeof res)
          console.log('响应键名:', Object.keys(res))
          console.log('状态码:', res.statusCode)
          console.log('响应头:', res.header)
          
          resolve({
            success: true,
            method: 'huawei_token',
            response: res,
            statusCode: res.statusCode,
            header: res.header
          })
        },
        fail: (err) => {
          console.log('华为云Token请求失败:', err)
          resolve({
            success: false,
            method: 'huawei_token',
            error: err
          })
        }
      })
      
      // 监听请求进度
      requestTask.onHeadersReceived((res) => {
        console.log('华为云Token请求收到响应头:', res)
      })
      
      requestTask.onProgressUpdate((res) => {
        console.log('华为云Token请求进度:', res)
      })
    })
  }

  // 运行所有测试
  async runAllTests() {
    console.log('=== 开始简单网络请求测试 ===')
    
    const results = []
    
    // 测试1: Promise方式
    const result1 = await this.testPromiseRequest()
    results.push(result1)
    
    // 测试2: 回调方式
    const result2 = await this.testCallbackRequest()
    results.push(result2)
    
    // 测试3: requestTask方式
    const result3 = await this.testRequestTask()
    results.push(result3)
    
    // 测试4: 华为云Token请求
    const result4 = await this.testHuaweiTokenRequest()
    results.push(result4)
    
    console.log('=== 所有测试结果 ===')
    console.log('完整结果:', results)
    
    return results
  }
}

module.exports = new SimpleRequestTest()
